package com.itheima.reggie.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    //查询所有

    @Override
    public List<Category> findAll() {
        List<Category> categoryList = null;
        String cateKey = "category:" + "findAll";
        categoryList = (List<Category>) redisTemplate.opsForValue().get(cateKey);
        if (categoryList != null) {//如果存在则在redis中调用数据
            log.info("redis中调用");
            return categoryList;//返回结果
        } else {//如果不存在则在数据库中调用
            //调用mapper查询所有
            categoryList = categoryMapper.findAll();
            //将数据存入redis中
            redisTemplate.opsForValue().set(cateKey, categoryList);
            return categoryList;//返回结果
        }
    }

    //添加菜品
    @Override
    public void save(Category category) {
        //雪花加密ID
        long id = IdUtil.getSnowflake(1, 1).nextId();
        //传入id
        category.setId(id);
        //添加人员信息
        //category.setCreateUser(1L);//TODO
        // 修改人员信息
        //category.setUpdateUser(1L);
        //创建时间
        //category.setCreateTime(new Date());
        //修改时间
        //category.setUpdateTime(new Date());

        Set categoryKey = redisTemplate.keys("category:");//读取所有key
        redisTemplate.delete(categoryKey);//批量删除
        //调用mapper层添加
        categoryMapper.save(category);
    }

    //修改菜品

    @Override
    public void update(Category category) {
        //修改人信息
        //category.setUpdateUser(1L);//TODO


        //修改时间
        //category.setUpdateTime(new Date());
        //调用mapper层方法修改
        //categoryMapper.update(category);
        //自动填充需要使用官方的方法，否则不会修改

        Set categoryKey = redisTemplate.keys("category:");//读取所有key
        redisTemplate.delete(categoryKey);//批量删除
        categoryMapper.updateById(category);
    }

    //不抛异常的删除菜品信息
//    @Override
//    public ResultInfo delete(long id) {
//        //根据id查询菜品数量
//        Integer dishCount = categoryMapper.countDishByCategoryId(id);
//        if (dishCount > 0) {
//            return ResultInfo.error("该分类下有菜品，不允许删除");
//        }
//        //根据id查询套餐数量
//        Integer setmealCount = categoryMapper.countSetmealByCategoryId(id);
//        if (setmealCount > 0) {
//            return ResultInfo.error("该套餐下有菜品，不允许删除");
//        }
//
//        //分类下没有数据可以删除
//        categoryMapper.delete(id);
//        return ResultInfo.success();
//    }
    //抛异常的分类删除

    @Override
    public void delete(long id) {
        //根据id查询菜品数量
        Integer dishCount = categoryMapper.countDishByCategoryId(id);
        if (dishCount > 0) {
            //抛出异常提示信息
            throw new CustomException("该分类下有菜品，不允许删除");
        }
        //根据id查询套餐数量
        Integer setmealCount = categoryMapper.countSetmealByCategoryId(id);
        if (setmealCount > 0) {
            throw new CustomException("该套餐下有菜品，不允许删除");
        }

        Set categoryKey = redisTemplate.keys("category:");//读取所有key
        redisTemplate.delete(categoryKey);//批量删除
        //分类下没有菜品则可以删除
        categoryMapper.delete(id);

    }

    //新增菜品/套餐时查询分类信息
    @Override
    public List<Category> findByType(Integer type) {
        //构建lambda条件
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(Category::getType, type);//根据传入的type查询分类表中的type
        qw.orderByAsc(Category::getSort);//根据分类的sort升序
        //调用mapper层集合方法查询
        Set categoryKey = redisTemplate.keys("category:");//读取所有key
        redisTemplate.delete(categoryKey);//批量删除
        return categoryMapper.selectList(qw);
    }


}
